package org.linlinjava.litemall.admin.web;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallStoreTagCategory;
import org.linlinjava.litemall.db.domain.TagCategoryVo;
import org.linlinjava.litemall.db.service.LitemallTagCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.linlinjava.litemall.admin.service.LogHelper;
import javax.validation.constraints.NotNull;
import java.util.List;

import static org.linlinjava.litemall.admin.util.AdminResponseCode.TAGCATEGORY_NAME_EXIST;

/**
 * @Description:	标签类别管理
 * @Author:			Coach tam
 * @Company:		坚持灵活  灵活坚持
 * @CreateDate:		2021-4-11 9:29:06
 */
@RestController
@RequestMapping("/admin/tagcategory")
@Validated
public class AdminTagCategoryController {
    private final Log logger = LogFactory.getLog(AdminTagCategoryController.class);

    @Autowired
    private LitemallTagCategoryService tagcategoryService;

    @Autowired
    private LogHelper logHelper;

    @RequiresPermissions("admin:tagcategory:list")
    @RequiresPermissionsDesc(menu = {"商场管理", "标签类别管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(String name,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        List<LitemallStoreTagCategory> litemallStoreTagCategoryList = tagcategoryService.querySelective(name, page, limit, sort, order);
        return ResponseUtil.okList(litemallStoreTagCategoryList);
    }

    @RequiresPermissions("admin:tagcategory:all")
    @RequiresPermissionsDesc(menu = {"商场管理", "标签类别管理"}, button = "查询所有")
    @GetMapping("/all")
    public Object all() {
        List<TagCategoryVo> litemallStoreTagCategoryList = tagcategoryService.findAll();
        return ResponseUtil.ok(litemallStoreTagCategoryList);
    }
    @RequiresPermissions("admin:tagcategory:read")
    @RequiresPermissionsDesc(menu = {"商场管理", "标签类别管理"}, button = "详情")
    @GetMapping("/detail")
    public Object litemallStoreTagCategoryDetail(@NotNull Integer id) {
    	LitemallStoreTagCategory litemallStoreTagCategory=tagcategoryService.findById(id);
        return ResponseUtil.ok(litemallStoreTagCategory);
    }
    @RequiresPermissions("admin:tagcategory:update")
    @RequiresPermissionsDesc(menu = {"商场管理", "标签类别管理"}, button = "编辑")
    @PostMapping("/update")
    public Object litemallStoreTagCategoryUpdate(@RequestBody LitemallStoreTagCategory litemallStoreTagCategory) {
        return ResponseUtil.ok(tagcategoryService.updateById(litemallStoreTagCategory));
    }

    @RequiresPermissions("admin:tagcategory:create")
    @RequiresPermissionsDesc(menu = {"商场管理", "标签类别管理"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody LitemallStoreTagCategory entity) {
        Object error = validate(entity);
        if (error != null) {
            return error;
        }

        String name = entity.getName();
        List<LitemallStoreTagCategory> list = tagcategoryService.findTagcategory(name);
        if (list.size() > 0) {
            return ResponseUtil.fail(TAGCATEGORY_NAME_EXIST, "已经存在");
        }


        tagcategoryService.add(entity);
        logHelper.logAuthSucceed("添加标签类别管理", name);
        return ResponseUtil.ok(entity);
    }

    private Object validate(LitemallStoreTagCategory entity) {
        String name = entity.getName();
        if (StringUtils.isEmpty(name)) {
            return ResponseUtil.badArgument();
        }

        return null;
    }


    @RequiresPermissions("admin:tagcategory:delete")
    @RequiresPermissionsDesc(menu = {"商场管理", "标签类别管理"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody LitemallStoreTagCategory entity) {
        Integer id = entity.getId();
        if (id == null) {
            return ResponseUtil.badArgument();
        }

        tagcategoryService.deleteById(id);
        logHelper.logAuthSucceed("删除标签类别管理", entity.getName());
        return ResponseUtil.ok();
    }

}
